<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Cart extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('products_m');
        $this->load->model('cart_m');
    } 

    public function index() {
        $temp = array();
        $temp['css'] = array('header','cart');
        //$temp['js'] = array('home');
        $this->load->view('open_v',$temp);
        $data['cart'] = $this->cart_m->get();
        $this->load->view('cart/list_v',$data);
        $this->load->view('close_v');
    }
    
    public function registration(){
        $temp = array();
        $temp['css'] = array('header','cart','validation');
        //$temp['js'] = array('home');
        $this->load->view('open_v',$temp);
        $this->load->view('cart/registration_v');
        $this->load->view('close_v');
    }
    
    public function submit(){
        $input['first_name'] = $this->input->post('first_name');
        $input['last_name'] = $this->input->post('last_name');
        $input['address'] = $this->input->post('address');
        $input['city'] = $this->input->post('city');
        $input['state'] = $this->input->post('state');
        $input['country'] = $this->input->post('country');
        $input['postal'] = $this->input->post('postal');
        $input['phone'] = $this->input->post('phone');
        $input['email'] = $this->input->post('email');
        $input['additional'] = $this->input->post('additional');
        //shipping
        $input['first_name_s'] = $this->input->post('first_name_s');
        $input['last_name_s'] = $this->input->post('last_name_s');
        $input['address_s'] = $this->input->post('address_s');
        $input['city_s'] = $this->input->post('city_s');
        $input['state_s'] = $this->input->post('state_s');
        $input['country_s'] = $this->input->post('country_s');
        $input['postal_s'] = $this->input->post('postal_s');
        $this->cart_m->add_purchase($input);
        $this->cart_m->clear();
        //send notification
        $this->load->model('utilities_m');
        $this->utilities_m->new_order_notification('acientid@hotmail.com');
        $this->session->set_flashdata('cart_complete',1);
        redirect('products');
    }
    
    //PRODUCTS CART
    public function add($product_id=null,$amount=null){
        if(empty($product_id) || empty($amount)){
            show_404();
        }
        $this->cart_m->add($product_id,$amount);
    }
    
    public function delete($product_id=null){
        if(empty($product_id)){
            show_404();
        }
        $this->cart_m->delete($product_id);
        redirect('cart');
    }

}